<?php
include_once("conn.php");
session_start();

?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mycss3.css" />
<meta charset="utf-8">
<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function(){
	$("#btn1").click(function(){
		$("#Login").slideToggle("slow");
		$("#Login_go").slideToggle("slow");
	});
	$("#go_back").click(function(){
		$("#Login").slideToggle("slow");
		$("#Login_go").slideToggle("slow");
	});
	$("#login1").click(function(){
		$("#Login").slideToggle("fast");
		$("#Login_go").slideToggle("fast");
	});
	
	var isDoingLogin = false;
	$("#bt_login").click(function(){
		var name1=$("#username").val();
		var pass1=$("#password").val();

		if (isDoingLogin)
		{
			return;
		}
		isDoingLogin = true;

		$.ajax({
			type:"POST",
			url:"login_process.php",
			dataType:"json",
			data:{"name2":name1,"pass2":pass1},
			beforeSend:function(){
				var div1="<p>正在登录中，请稍候...</p>";
				$("#Login_go").append(div1);
			},
			success:function(json){
				$("#b").remove();
				var div2="<li style='float:right;padding:0 3px;'>"+"<a href='manage.php'>"+"欢迎回来"+json.u1+"</a></li>";
				$("#a").append(div2);
			},
			error: function() {
				isDoingLogin = false;
			}

		});
	});
});

</script>
<script>
	function show_add_link(abc)
	{
		var obj=document.getElementById(abc);
		if(obj.style.display=='none')
		{
			obj.style.display='block';
		}
		else
		{
			obj.style.display='none';
		}
	}

	

	function cate_modify(a)
	{
		var obj=document.getElementById(a);
		if(obj.style.display=="none")
		{
			obj.style.display='block';
		}
		else
		{
			obj.style.display='none';
		}
	}

	function judge(a)
	{
		var obj=document.getElementById(a);
		if(obj.style.display=='block')
			return 1;
		else
			return 0;
	}
	

	function show_edit_link(abc)
	{
		var obj2=document.getElementById(abc);
		if(obj2.style.display=="none")
		{
			obj2.style.display="block";
		}
		else
		{
			obj2.style.display="none";
		}
	}
</script>
</head>
<body>

<div id="Header">
	<ul id="a">
		<li><a href="index.php">首页</a></li>
		<li><a href="public.php">发现</a></li>
		<?php
		if($_SESSION["current_user"] == NULL )
		{
			echo '<li style="float:right;padding:0 3px;"><a href="reg.php">注册</a></li>';
			echo '<li id="b" style="float:right;padding:0 3px;"><a id="login1" href="#">登录</a></li>';
		}
		else
		{
			echo '<li style="float:right;padding:0 3px;"><a href="login_out.php">注销</a></li>';
			echo '<li style="float:right;padding:0 3px;"><a href="manage.php">'.$_SESSION["current_user"].'</a></li>';
		}
		?>
	</ul>

</div>

<div id="Login">
	<form>
		<input type="button" name="btn1" id="btn1" value="登录" />
	</form>
</div>

<div id="Login_go" style="display:none;">
	<form action="login_go.php" method="post" id="login_form">
		<tr>用户名<input type="text" name="username" id="username" /></tr>
		<tr>密码：<input type="password" name="password" id="password"/></tr>
		<tr><input type="button" value="登录" name="login" id="bt_login" /></tr>
		<tr><input type="reset" value="重置"  /></tr>
	</form>
	<tr><input type="button" id="go_back" value="返回" /></tr>
</div>

<div id="Content">
	<?php
	
	function show()
	{
		echo "<div class='heading' align='middle' float:right>";
			echo $_POST["newcategory"];
			echo "<div class='controls'>";
				echo "<a href='#' class='add_link' onclick=show_add_link('add_cate_link".$_POST["newcategory"]."');return false;>";
				echo "<img src='/元素/image/tiny_plus.png'>";
				echo "</a>";
				echo "<a href='#' class='divider' >";
				echo "</a>";
				echo "<a href='#' class='modify' onclick=cate_modify('".$_POST["newcategory"]."');return false; >";
				echo "<img src='/元素/image/tiny_gear.png'>";
				echo "</a>";
			echo "</div>";
		echo "</div>";

		/* 以下为增加分类中的增加网址模块 */
		echo "<div class='add_link_form'  id='add_cate_link".$_POST["newcategory"]."' style='padding-top:15px;padding-right:15px;padding-bottom-15px;padding-left:15px;display:none;' align='middle'>";
		
		echo "<form action='index.php' method='post'>";
		
		echo "<input type='hidden' name='is_link_submit' value='yes' />";
		echo "<input type='hidden' name='cname' value='$cid' />";
		echo "<input type='text' name='link_name' placeholder='input your link name' />";
		echo "<input type='text' name='link_address' placeholder='create your new link' />";
		echo "<input type='submit' value='create link' />";
		
		echo "</form>";
		
		echo "</div>";
		
		/* 以上为增加分类中的网址模块 */

	
		/* 以下为修改分类信息模块*/
		echo "<div class='edit_cate_form' id='".$_POST["newcategory"]."' style='display:none'; align='middle'> ";
		echo "<form action='cate_process.php' method='post'> ";
		
		echo "<input type='text' name='edit_cate_name' value='".$got["cate_name"]."' />";
		echo "<input type='hidden' name='delcate_action' value='".$got["cate_name"]."' /> ";
		echo "<input type='submit' name='save_cate' id='save_cate' value='save' />";
		echo "<input type='submit' name='del_cate' id='del_cate' value='delete'/>";
		echo "</form>";
		echo "</div>";

		/*  以上为修改分类信息模块*/
		

	}
	
	include_once("c_add_link.php");
	include_once("c_add_cate.php");
	include_once("conn.php");

	echo "<div class='wrapper'>";
		echo "<form method='post' action='index.php'>";
			echo "<input type='text' name='newcategory' class='cate' placeholder='在此输入要新创建的分类' />";
			echo "<input type='hidden' name='action' value='create' />";
			echo "<input type='submit' class='submit' value='create' name='submit'>";
		echo "</form>";
	echo "</div>";
	$sql="select cate_name,cid from category where category.name='".$_SESSION["current_user"]."'";
	$result = mysql_query($sql);
	echo "<div id='container'>";
	if($result)
	{
		while($got=mysql_fetch_array($result))
		{
			$cid=$got["cid"];
			$cname=$got["cate_name"];
			echo "<div class='heading'>";
				echo "<div class='change_cate_style' id='".$cid."' style='float:left;'>";
				echo $got["cate_name"];
				echo "</div>";
				//$cid=$got["cid"];
				echo "<div class='controls'>";
					echo "<a href='#' class='add_link' onclick=show_add_link('add_cate".$got["cate_name"]."');return false;>";
					echo "<img src='../元素/image/tiny_plus.png'>";
					echo "</a>";
					echo "<a href='#' class='divider' >";
					echo "</a>";
					echo "<a href='#' class='modify' onclick=cate_modify('".$got["cate_name"]."');return false;>";
					echo "<img src='/元素/image/tiny_gear.png'>";
					echo "</a>";
				echo "</div>";
			echo "</div>";
			/* 以下为增加分类中的增加网址模块 */
			echo "<div class='add_link_form'  id='add_cate".$got["cate_name"]."' style='padding-top:15px;padding-right:15px;padding-bottom-15px;padding-left:15px;display:none;'>";
			
			echo "<form action='index.php' method='post'>";
			
			echo "<input type='hidden' name='is_link_submit' value='yes' />";
			echo "<input type='hidden' name='cname' value='$cid' />";
			echo "<input type='text' name='link_name' placeholder='input your link name' />";
			echo "<input type='text' name='link_address' placeholder='create your new link' />";
			echo "<input type='submit' value='create link' />";
			
			echo "</form>";
			
			echo "</div>";
			
			/* 以上为增加分类中的网址模块 */
			
			/* 以下为修改分类信息模块*/
			echo "<div class='edit_cate_form' id='".$cname."' style='display:none';> ";
			echo "<form action='cate_process.php' method='post' > ";
			
			echo "<input type='text' class='cate_style' id='edit_cate_name' name='edit_cate_name' value='".$got["cate_name"]."' />";
			echo "&nbsp&nbsp&nbsp";
			echo "<input type='hidden' name='delcate_action' id='delcate_action' value='".$got["cate_name"]."' /> ";
			echo "<input type='hidden' name='cid2' id='cid2' value='".$cid."' />" ;
			echo "&nbsp&nbsp";
			echo "<input type='button' class='save_cate_style' name='save_cate' id='save_cate' value='save' onclick=\"change_cate('".$cname."',".$cid.");return false;\" />";
			echo "<input type='submit' name='del_cate' id='del_cate' value='delete'/>";


			echo "</form>";
			echo "</div>";

			/*  以上为修改分类信息模块*/
			

			$got_catename=$got["cate_name"];
			echo "<div class='link'>";
			$sql2="select category.cate_name,link.lid,webname,webaddress from category,cate_link,link where category.cid=cate_link.cid && link.lid=cate_link.lid ";
			mysql_query("set names utf-8");
			$result2=mysql_query($sql2);
			if($result2)
			{
				while($got_cate=mysql_fetch_array($result2))
				{
					if($got_cate["cate_name"]==$got_catename)
					{
						echo "<div class='link_line'>";
						echo "<div class='link_head'>";
							echo $got_cate["webname"];
							$temp_webname=$got_cate["webname"];
							echo "&nbsp";
							echo "&nbsp";
							echo $got_cate["webaddress"];
							echo "</div>";
							//$temp_webaddress=$got_cate["webaddress"];
							//$temp_lid=$got_cate["lid"];
							//$lid=$got_cate["lid"];
							echo "<div class='controls'>";
								echo "<a href='#' onclick=show_edit_link('edit_link_".$got_cate["lid"]."');>";
									echo "<img src=/元素/image/tiny_gear.png >";
								echo "</a>";
							echo "</div>";
						echo "</div>";
						/*以下为编辑链接FORM*/
						echo "<div id='edit_link_".$got_cate["lid"]."' style='display:none;' class='edit_link_'>"; 
							echo "<form method='post' action='link_process.php' name='".$got_cate["webname"]."'>";
							echo "<input type='text' name='alias' value='$got_cate[webname]' />";
							echo "<input type='text' name='alias_name' value='$got_cate[webaddress]'/>";
							echo "<input type='hidden' name='save' value='save' />";
							echo "<input type='hidden' name='".$got_cate["lid"]."' value='".$got_cate["webname"]."' />";
							echo "<input type='hidden' name='real_webname' value='".$got_cate["webname"]."' /> ";
							echo "<input type='submit' name='save_link' value='save' />";
							echo "<input type='submit' name='del_link' value='delete' />";
							echo "</form>";
						echo "</div>";
						/*以上为编辑链接FORM*/

					}

				}
			}
			else
			{
				echo "链接数据库读取失败";
			}
			

			


			echo "</div>";
		}
	}
	else
	{
		echo "读取分类数据库失败";
	}
	echo "</div>";

	$receive_new_cate=new addCate();

	if($_POST["action"]==create && $_POST["newcategory"]!=NULL && $_SESSION["current_user"]!=NULL )
	{
		
		$receive_new_cate->addC();
		show();
		echo "<meta http-equiv='refresh' content='0.5'; url='index.php' />";
	}
	else if( $_POST["action"]==create && $_SESSION["current_user"] == NULL )
	{
		echo "<script language='javascript'>alert('请先登录！');</script>";
	}

?>

</div>

<div id="Footer">
</div>


</body>
</html>